#include "buffermanager.h"
#include "dbobject.h"
#include "utils.h"
#include "log.h"
#include <string>

std::map<uint32_t, std::filebuf> BufferManager::mBuffers;

BufferManager& BufferManager::getInstance() {
    static BufferManager bm;
    return bm;
}

BufferManager::~BufferManager() {
    for (auto it = mBuffers.begin(); it != mBuffers.end(); it++) {
        it->second.close();
    }
}

std::unique_ptr<char> BufferManager::GetBufferData(uint32_t vid, uint32_t start, uint32_t& count) {
    if (mBuffers.find(vid) == mBuffers.end()) {
        std::string filename;
        auto names = storage.select(&VideoMetaData::path, where(c(&VideoMetaData::id) == vid));
        for (auto& name : names) {
            filename = name;
        }

		std::cout << filename << std::endl;
        mBuffers[vid] = std::filebuf();
        mBuffers[vid].open(filename, std::ios_base::in | std::ios_base::binary);
    }
    //设置指针位置
    mBuffers[vid].pubseekpos(start, std::ios_base::in);
    //读取filebuf
    char* temp = new char[BATCH_SIZE];
    count = mBuffers[vid].sgetn(temp, BATCH_SIZE);
    //std::string encode;
    //encode.resize(Base64::EncodedLength(BATCH_SIZE));
    //if(!Base64::Encode(temp, BATCH_SIZE, &encode[0], encode.size())) {
        //ALOGE("Encode ERROR!!!!");
    //}
	std::unique_ptr<char> encode(temp);
    return encode;
}

BufferManager::BufferManager() {
    
}
